package com.example.studentmanager.service.impl;

import com.example.studentmanager.mapper.AdministratorMapper;
import com.example.studentmanager.model.Administrator;
import com.example.studentmanager.service.AdministratorService;
import com.example.studentmanager.utils.BaseDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;



@Component
@Service
public class AdministratorServiceImpl implements AdministratorService {

    @Autowired
    Administrator administrator;

    SqlSession sqlSession= new BaseDao().getSession();
    AdministratorMapper administratorMapper=sqlSession.getMapper(AdministratorMapper.class);

    @Override
    public String  saveAdministrator(Administrator administrator) {

        try {
            int result=administratorMapper.addAdministrator(administrator);
            if(result<1){
                return "fail";
            }else {
               return "success";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Administrator searchAdminByUserName(String username) {
        SqlSession sqlSession = new BaseDao().getSession();
        Administrator admin=new Administrator();
        AdministratorMapper administratorMapper = sqlSession.getMapper(AdministratorMapper.class);
        try {
            administrator = administratorMapper.selectAdminByUsername(username);
            admin=administrator;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public void changeAdministrator(Administrator administrator) {
        try {
            administratorMapper.updateAdmin(administrator);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAdministrator(Administrator administrator) {

        try {
            administratorMapper.deleteAdministrator(administrator);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Administrator> searchAllAdmin() {
        List<Administrator> administrators=administratorMapper.searchAllAdmin();
        return administrators;
    }

    @Override
    public List<Administrator> blurSearchAdmin(String str) {
        String str1="%"+str+"%";
        List<Administrator>  administrators=administratorMapper.blurSelectAdmin(str1);
        return administrators;
    }


}
